/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author quyennb
 */
public class HandleDataTableCampaignDateV2 {

    public static final String KEYSPACE                         =   "AdsDataV2";
    public static final String ColumFamly                       =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf     =   "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf      =   "BannerViewCountCf";

    public static final String ColumFamlyCampaignClickCountCf       =   "CampaignCountCf";
    public static final String ColumFamlyCampaignViewCountCf        =   "CampaignViewCountCf";

    public void handleData() throws ParseException, InterruptedException
    {
       long first_time = System.currentTimeMillis();
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);
        MysqlDataManager67 mysql67 = new MysqlDataManager67();             
        Map<Integer, String> uv_uc = new HashMap<>();                       
        Map<String, String> listcamps = new HashMap<>();
        String currentDate = HandleDataTableDomainDate.handleDate();//2012-07-24
        //String currentDate = "2013-08-05";
        
        StringBuilder sbff = new StringBuilder();
        uv_uc = mysql67.getUvUcByCampaignid(currentDate);
                
        listcamps = mysql67.getAllCampaignByDate(currentDate);
        Iterator it=listcamps.entrySet().iterator();
        
        while(it.hasNext())
        {
            Map.Entry m =(Map.Entry)it.next();
            String valuekey = (String)m.getKey();
            String campid = valuekey.split("_")[0];
            String valCamp = (String)m.getValue();
            //if(campid.contains(sbff))
            String str = getClickInMonth(valCamp,cassAct, currentDate, campid, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf,uv_uc);
            sbff.append(str);
            
        }

         //xu li cho du lieu cuoi
        if(sbff.toString().length() > 1)
        {
            String relStr = sbff.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            
            HandleImportData hid = new HandleImportData();
            String pathFile="";
            try {
                pathFile = hid.outToCsvCampaign(valInsert);
                Debug.println("pathFile:"+pathFile);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            try {
                //get sys_date_cpc
                String strDate =  mysql67.getSysDateCPC();                
             
                MysqlDataManager67 mysql672 = new MysqlDataManager67(false);
                mysql672.beforeLoadData();
                
                mysql672.dropPartition(CONSTANT.V3_CAM_DATE_CS, strDate);
                                 
                mysql672.loadToV3CamDateFromFile(pathFile);
                //lock table 
                //mysql672.unlockTable();
                mysql672.shutdownConnection();                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        //close connection
        mysql67.shutdownConnection();
                
        long last_time = System.currentTimeMillis();
        System.out.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableCampaignDate");

    }

     public static String getClickInMonth(String valCamp,CassandraAction cassAct,String currentDay ,String key,String columnFamilyClick,String columnFamilyView,Map uv_uc) throws ParseException{
        StringBuilder sb = new StringBuilder();
        /*String changeDay = currentDay.split("-")[2]+ "-" + currentDay.split("-")[1]+"-" + currentDay.split("-")[0];
        String date = "00:00:00 " + changeDay ;
        long start_time = TimeUtil.longToDateWithHour(date);
        //them 1 ngay
        long finish_time = start_time + 24*60*60*1000;
        long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);        
        long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);*/
        long countView          = Long.valueOf(valCamp.split("_")[0]);
        long countClick         = Long.valueOf(valCamp.split("_")[1]);
        long countRealclick     = Long.valueOf(valCamp.split("_")[2]);
        long countchargeclick   = Long.valueOf(valCamp.split("_")[3]);
        long money              = Long.valueOf(valCamp.split("_")[4]);
        //check neu campid < 9 chu so
        if(Long.valueOf(key) < 1000000000){
            if(uv_uc.containsKey(Integer.valueOf(key)))
            {
                String uValue = (String) uv_uc.get(Integer.valueOf(key));
                String uc = uValue.split("_")[0];
                String uv = uValue.split("_")[1];

                //if((countView >0))
                //{
                    DecimalFormat df = new DecimalFormat("##0.00");
                    String ctr ="";
                    if(countView == 0){
                        ctr =  "0";
                    } else {
                        ctr = df.format(Float.valueOf(countClick)/Float.valueOf(countView)*100);
                    }
                    String str = "('" + key + "'," + countClick + "," + countView+ "," + countRealclick+ "," +  countchargeclick+ "," + countView +"," + Integer.valueOf(uv) + "," + Integer.valueOf(uc) + ",'" + currentDay + "'," + ctr +"," + money +",0)";
                    sb.append(str).append(",");
                //}

             } else
             {
                //if((countView >0))
                //{
                    DecimalFormat df = new DecimalFormat("##0.00");
                    String ctr ="";
                    if(countView == 0){
                        ctr =  "0";
                    } else {
                        ctr = df.format(Float.valueOf(countClick)/Float.valueOf(countView)*100);
                    }
                    String str = "('" + key + "'," + countClick + "," + countView+ "," + countRealclick+ "," + countchargeclick+ "," + countView + ",0,0,'" + currentDay + "'," + ctr +"," + money +",0)";
                    sb.append(str).append(",");
                //}
             }
         }
        //mysqldatamanager.insertDataCampaignDay(key, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));        
        return sb.toString();
        
    }        
}
